﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace HRM.Web.account
{
    public partial class login : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["loggedout"] == "1")
            {
                Session.Clear();

            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsAuthenticate())
                Response.Redirect("~/forms/dashboard.aspx");
            else
            {
                divAlert.Visible = true;
                ltrMessage.Text = "Username or password is incorrect.";
            }
        }

        protected Boolean IsAuthenticate()
        {
            BLL.Login objLogin = new BLL.Login();
            DataTable dt = new DataTable();
            dt=objLogin.IsAuthentic(txtUsername.Text.Trim(), txtPassword.Text.Trim());

            if (dt.Rows.Count>0)
            {
                Session["UserID"] = dt.Rows[0]["ID"].ToString();
                String strUserData = "Admin";

                FormsAuthentication.Initialize();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,
                        txtUsername.Text.Trim(),
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        true,
                        strUserData,
                        FormsAuthentication.FormsCookiePath);

                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(
               FormsAuthentication.FormsCookieName,
               hash);

                if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

                Response.Cookies.Add(cookie);
                //Session["UserId"] = dr_cls_tblUser["User_Id"].ToString();
                //Session["Username"] = dr_cls_tblUser["User_Name"].ToString();
                return true;
            }
            return false;
          
        }
    }
}